package com.cqq.common.core.util;

import java.util.Random;

/**
 * <h3>随机字符串生成工具类</h3>
 * 
 * @author xijq
 * @version 1.0.0
 * 
 */
public final class RandomGenerator {
	
	private RandomGenerator(){
	}
	
	/**
	 * 类型：纯数字
	 */
	public static final int TYPE_NUMBER = 0;
	
	/**
	 * 类型：纯字母（大小写随机）
	 */
	public static final int TYPE_LETTER = 1;
	
	/**
	 * 类型：纯字母（小写）
	 */
	public static final int TYPE_LETTER_LOWERCASE = 11;
	
	/**
	 * 类型：纯字母（大写）
	 */
	public static final int TYPE_LETTER_UPPERCASE = 12;
	
	/**
	 * 类型：数字字母混合（大小写随机）
	 */
	public static final int TYPE_MIXED = 2;
	
	/**
	 * 类型：数字字母混合（小写）
	 */
	public static final int TYPE_MIXED_LOWERCASE = 21;
	
	/**
	 * 类型：数字字母混合（大写）
	 */
	public static final int TYPE_MIXED_UPPERCASE = 22;
	
	public static boolean verifyRandomNumber(String num){
		try{
			int length=num.length();
			int m=length*9;
			int n=0;
			for(int i=0; i<length-1; i++){
				n+=Integer.valueOf(String.valueOf(num.charAt(i)));
			}
			if((m-n)%10 == Integer.valueOf(String.valueOf(num.charAt(length-1)))) {
				return true;
			} else {
				return false;
			}
		}
		catch(Exception e){
			return false;
		}
	}
	
	public static boolean verifyRandomLetter(String letter){
		try{
			int length=letter.length();
			int m=length*25;
			int n=0;
			for(int i=0,j=0; i<length-1; i++){
				j=letter.charAt(i)-65;
				if(!(j<26 && j>=0)){
					j=letter.charAt(i)-97;
				}
				n+=j;
			}
			if((m-n)%10 == letter.charAt(length-1)-65
					|| (m-n)%10 == letter.charAt(length-1)-97)
				return true;
			else
				return false;
		}
		catch(Exception e){
			return false;
		}
	}
	
	public static boolean verifyRandomNumberLetter(String code){
		//暂时不实现数字与字母混合验证码的校验
		return true;
	}
	
	public static boolean verifyRandomString(int type, String code){
		if(type == TYPE_NUMBER){
			return verifyRandomNumber(code);
		}
		else if(type == TYPE_LETTER){
			return verifyRandomLetter(code);
		}
		else if(type == TYPE_MIXED){
			return verifyRandomNumberLetter(code);
		}
        return verifyRandomNumber(code);
    } 
	
	public static String getRandomNumber(int length){
		StringBuffer sb=new StringBuffer();
		Random rnd=new Random();
		int m=length*9;
		int n=0;
		for(int i=0,j=0; i<length-1; i++){
			j=rnd.nextInt(10);
			n+=j;
			sb.append(j);
		}
		//最后一位特殊处理，供校验使用
		sb.append((m-n)%10);
		return sb.toString();
	}
	
	public static String getRandomLetter(int length){
		StringBuffer sb=new StringBuffer();
		Random rnd=new Random();
		int m=length*25;
		int n=0;
		int choice=0;
		for(int i=0,j=0; i<length-1; i++){
			//字母A=65,a=97
			choice = rnd.nextInt(2) % 2 == 0 ? 65 : 97;
			j=rnd.nextInt(26);
			n+=j;
			sb.append((char)(choice + j));
		}
		choice = rnd.nextInt(2) % 2 == 0 ? 65 : 97;
		//最后一位特殊处理，供校验使用
		sb.append((char)(choice+(m-n)%10));
		return sb.toString();
	}
	
	public static String getRandomNumberLetter(int length){
		StringBuffer sb=new StringBuffer();
		Random rnd=new Random();
		String charOrNum=null;
		int choice=0;
		for(int i=0; i<length; i++){
			// 输出字母还是数字
			charOrNum = rnd.nextInt(2) % 2 == 0 ? "char" : "num";
			if("char".equalsIgnoreCase(charOrNum)) {
	            choice = rnd.nextInt(2) % 2 == 0 ? 65 : 97;  
	            sb.append((char)(choice + rnd.nextInt(26)));     
	        }     
	        else if("num".equalsIgnoreCase(charOrNum)){     
	            sb.append(rnd.nextInt(10));     
	        }     
		}
		return sb.toString();
	}
	
	public static String getRandomString(int type, int length){
		if(type == TYPE_NUMBER){
			return getRandomNumber(length);
		}
		else if(type == TYPE_LETTER){
			return getRandomLetter(length);
		}
		else if(type == TYPE_MIXED){
			return getRandomNumberLetter(length);
		}
        return getRandomNumber(length);
    } 
}
